# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=boost
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.67.0
_boostver=${pkgver//./_}
pkgrel=1
pkgdesc="Free peer-reviewed portable C++ source libraries (mingw-w64)"
arch=('any')
url="https://www.boost.org/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3")
options=('strip' 'buildflags' 'staticlibs')
source=(https://downloads.sourceforge.net/sourceforge/boost/boost_${_boostver}.tar.bz2
        boost-1.48.0-add-bjam-man-page.patch
        boost-1.50.0-fix-non-utf8-files.patch
        boost-1.58.0-pool.patch
        boost-1.63.0-python-test-PyImport_AppendInittab.patch
        boost-1.57.0-build-optflags.patch
        boost-1.57.0-mpl-print.patch
        boost-1.58.0-pool-test_linking.patch
        boost-1.57.0-python-abi_letters.patch
        boost-1.57.0-python-libpython_dep.patch
        boost-1.60.0-mingw-context.patch
        boost-1.61.0-fix-undefined-referense-to-codecvt.patch
        boost-include-intrin-h-on-mingw-w64.patch
        using-mingw-w64-python.patch
        msys2-mingw-folders-bootstrap.patch
        boost-1.66.0-fix-qualifier.patch)
sha256sums=('2684c972994ee57fc5632e03bf044746f6eb45d4920c343937a465fd67a5adba'
            'c5eae6354693c6b4e6364d4cf6f06c6c3de4c28486bf812cbd291f4410126cf8'
            '8d048e8ecee8b7feaa8651d6bc65d63838df240626619a4fb8738512043c3eb1'
            '4551ba9edf64c8ccdab4f4890c20e2e8cf6d4ffa1169d251df11e30d25b886b8'
            'b22196b6415f5e1c0fe56b49a12ea7c20073b15a5f31907f363c7be38d70d628'
            'bb83d991f900a1cfd6a5aa874cbc33f76f3d6ce6388fd996886fdabe79b1f74c'
            'd4be505bf6cd08b9da232d0311bfda2820dd75cf8911ed07b72eddc418c73614'
            'b15743957aa1c7eb1e861dfe96247e50b7afd96258d960be403851ce62923c87'
            'cf46e9b791b45937d5af26b0f35dcdfa416cd9b62ef6c29e3064464835634628'
            '01758929643f92530512230d37df9793e6481cd6ce6310e3a79cee5ba287858c'
            '9ac0fc0306114d709958ae6e17fdd38616fa7c352d10346f099b51ef49d0ec4e'
            '2d5a82277f271612421f05c3b1a1e55fe736a18b581920eea12b54767d74edad'
            '9a269375b8eb262b93fb18ea6b2ac8c97b16fb71b8bab900faa4c2df2816f2eb'
            '0dd6346d369850aad13bf8d9bc2f0abc16d4b0586e34d61fab11f65d3d7fa9d4'
            '81f74c2eafbe004bcbb863cc3cd74571adcffed1169dde86645db4441be236e1'
            'af7eea3c0dc1329d8735bd38d0b43391ba5931c61e07f117cfe7d9b06a4d4d73')

prepare() {
  cd "${srcdir}/boost_${_boostver}"

  # https://svn.boost.org/trac/boost/ticket/6150
  patch -p1 -i ${srcdir}/boost-1.50.0-fix-non-utf8-files.patch

  # Add a manual page for bjam, based on the on-line documentation:
  # https://www.boost.org/boost-build2/doc/html/bbv2/overview.html
  rm tools/build/v2/doc/bjam.1 || true
  msg2 "boost-1.48.0-add-bjam-man-page.patch"
  patch -p1 -i ${srcdir}/boost-1.48.0-add-bjam-man-page.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=828856
  # https://bugzilla.redhat.com/show_bug.cgi?id=828857
  # https://svn.boost.org/trac/boost/ticket/6701
  msg2 "boost-1.58.0-pool.patch"
  patch -p0 -i ${srcdir}/boost-1.58.0-pool.patch

  # https://svn.boost.org/trac/boost/ticket/5637
  msg2 "boost-1.57.0-mpl-print.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-mpl-print.patch

  # https://svn.boost.org/trac/boost/ticket/9038
  msg2 "boost-1.58.0-pool-test_linking.patch"
  patch -p1 -i ${srcdir}/boost-1.58.0-pool-test_linking.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1102667
  msg2 "boost-1.57.0-python-libpython_dep.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-libpython_dep.patch
  msg2 "boost-1.57.0-python-abi_letters.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-python-abi_letters.patch
  msg2 "boost-1.60.3-python-test-PyImport_AppendInittab.patch"
  patch -p1 -i ${srcdir}/boost-1.63.0-python-test-PyImport_AppendInittab.patch

  # https://bugzilla.redhat.com/show_bug.cgi?id=1190039
  msg2 "boost-1.57.0-build-optflags.patch"
  patch -p1 -i ${srcdir}/boost-1.57.0-build-optflags.patch

  # https://svn.boost.org/trac/boost/ticket/7262
  msg2 "boost-1.60.0-mingw-context.patch"
  patch -p1 -i ${srcdir}/boost-1.60.0-mingw-context.patch

  # https://svn.boost.org/trac/boost/ticket/12205
  msg2 "boost-1.61.0-fix-undefined-referense-to-codecvt.patch"
  patch -p1 -i ${srcdir}/boost-1.61.0-fix-undefined-referense-to-codecvt.patch

  # Fix FTBFS on recent mingw-w64 and also use intrinsics based
  # versions of the Interlocked symbols which are better optimized
  msg2 "boost-include-intrin-h-on-mingw-w64.patch"
  patch -p0 -i ${srcdir}/boost-include-intrin-h-on-mingw-w64.patch

  # Use out own build Python
  msg2 "using-mingw-w64-python.patch"
  patch -p1 -i ${srcdir}/using-mingw-w64-python.patch

  msg2 "msys2-mingw-folders-bootstrap.patch"
  patch -p1 -i ${srcdir}/msys2-mingw-folders-bootstrap.patch
  
  msg2 "boost-1.66.0-fix-qualifier.patch"
  patch -p1 -i ${srcdir}/boost-1.66.0-fix-qualifier.patch
}

build() {
  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  lndir "${srcdir}/boost_${_boostver}" "${srcdir}/build-${MINGW_CHOST}"

  cd "${srcdir}/build-${MINGW_CHOST}"

  _py2ver=$(${MINGW_PREFIX}/bin/python2 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  _py3ver=$(${MINGW_PREFIX}/bin/python3 -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  echo "using python : ${_py2ver} : ${PREFIX_DEPS}/bin/python2.exe : ${PREFIX_DEPS}/include/python${_py2ver}  : ${PREFIX_DEPS}/lib/python${_py2ver}/config ;"             >> ./tools/build/src/user-config.jam
  echo "using python : ${_py3ver} : ${PREFIX_DEPS}/bin/python3.exe : ${PREFIX_DEPS}/include/python${_py3ver}m : ${PREFIX_DEPS}/lib/python${_py3ver}/config-${_py3ver}m ;" >> ./tools/build/src/user-config.jam
  #echo "using gcc : : ${PREFIX_DEPS}/bin/g++.exe : <rc>${PREFIX_DEPS}/bin/windres.exe ;" >> ./tools/build/src/user-config.jam

  ./bootstrap.sh --with-icu=${MINGW_PREFIX} --with-python=${MINGW_PREFIX} --with-toolset=gcc

  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac

  if check_option "debug" "y"; then
      local variant=debug
  else
      local variant=release
  fi

  ./b2 \
    address-model=${_model} \
    link=shared,static \
    pch=off \
    runtime-link=shared \
    threading=multi \
    threadapi=win32 \
    toolset=gcc \
    variant=${variant} \
    python=2.7,3.6 \
    --debug-configuration \
    --prefix=${MINGW_PREFIX} \
    --layout=tagged \
    --without-mpi \
    -sHAVE_ICU=1 \
    -sICU_PATH=${MINGW_PREFIX} \
    -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
    -sICONV_PATH=${MINGW_PREFIX} \
    -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
    -sNO_BZIP2 \
    -sBZIP2_BINARY=bz2 \
    -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
    -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
    -sNO_ZLIB \
    -sZLIB_BINARY=z \
    -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
    -sZLIB_LIBPATH=${MINGW_PREFIX}/lib \
    -d2 \
    -j$(nproc)
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  case ${CARCH} in
    i686)
      local _model=32
    ;;
    x86_64)
      local _model=64
    ;;
  esac

  if check_option "debug" "y"; then
      local variant=debug
  else
      local variant=release
  fi

  ./b2 \
      address-model=${_model} \
      link=shared,static \
      runtime-link=shared \
      threading=multi \
      threadapi=win32 \
      toolset=gcc \
      variant=${variant} \
      python=2.7,3.6 \
      --prefix=${pkgdir}${MINGW_PREFIX} \
      --layout=tagged \
      --without-mpi \
      -sHAVE_ICU=1 \
      -sICU_PATH=${MINGW_PREFIX} \
      -sICU_LINK="-L${MINGW_PREFIX}/lib -licuuc -licuin -licudt" \
      -sICONV_PATH=${MINGW_PREFIX} \
      -sICONV_LINK="-L${MINGW_PREFIX}/lib -liconv" \
      -sNO_BZIP2 \
      -sBZIP2_BINARY=bz2 \
      -sBZIP2_INCLUDE=${MINGW_PREFIX}/include \
      -sBZIP2_LIBPATH=${MINGW_PREFIX}/lib \
      -sNO_ZLIB \
      -sZLIB_BINARY=z \
      -sZLIB_INCLUDE=${MINGW_PREFIX}/include \
      -sZLIB_LIBPATH=${MINGW_PREFIX}/lib \
      install

  mkdir -p ${pkgdir}${MINGW_PREFIX}/bin
  mv ${pkgdir}${MINGW_PREFIX}/lib/*.dll ${pkgdir}${MINGW_PREFIX}/bin
}
